package com.mylibrary.controller;

import com.mylibrary.domain.ReaderInfo;
import com.mylibrary.domain.ReaderLogin;
import com.mylibrary.service.LogInService;
import com.mylibrary.service.RegisterService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;

@Controller
public class MyController {
    @Resource
    private RegisterService registerService;
    @RequestMapping(value="/load",method = RequestMethod.POST)
    @ResponseBody
    public String load(ReaderInfo readerInfo) {
        Date date=new Date();
        System.out.println(date);
        SimpleDateFormat sd=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        readerInfo.setRegisterDate(sd.format(date));
        int count = registerService.readerRegister(readerInfo);
        if(count!=1){
            return "<h2>该用户名已存在,注册失败<h2/>";
        }
        return "<h2>注册成功<h2/>";
    }
    @Resource
    private LogInService logInService;
    @RequestMapping(value = "/login")
    @ResponseBody
    public String logIn(ReaderLogin readerLogin){
        int result=logInService.logIn(readerLogin);
        System.out.println(result);
        if(result!=1){
            return "<h2>用户名或密码不正确，请重新输入<h2/><br/>" +
                    "<h2><a href=\"http://localhost:8080/library\">重新登录<a/><h2/>" ;
        }else{
            return "<h2>登录成功！！欢迎使用图书系统!!!<h2/>";
        }
    }
}
